<template>
    <view>
        <view class="goods-list">
            <view class="goods-item">
                <view class="flex-row flex-y-center score-row" style="padding: 20rpx 30rpx">
                    <image class="goods-pic" :src="orderinfo.store.logo"></image>
                    <view style="margin-left: 30rpx">{{ orderinfo.store.name }}</view>
                </view>
                <view class="pjxx">
                    <image
                        class="star-image"
                        :src="key > item ? (key - item == 0.5 ? halfSrc : selectedSrc) : normalSrc"
                        :style="'left: ' + item * 70 + 'rpx'"
                        v-for="(item, index) in stars"
                        :key="index"
                    >
                        <view @tap="selectLeft" class="item" :data-key="item + 1" style="left: 0rpx"></view>
                    </image>
                </view>
                <view class="banner">
                    <view class="pjnr">
                        <textarea @input="contentInput" :data-index="index" maxlength="100" placeholder="要评价的内容..." placeholderStyle="color:#999;"></textarea>
                    </view>
                    <view class="mdtp flex-row" v-if="!sctp">
                        <image @tap="sctpFun" src="https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/zxj.png"></image>
                        <view class="flex-grow-1 flex-col">
                            <view class="flex-grow-1">上传图片</view>
                            <view class="flex-grow-0">内容丰富的评价有机会成为优质评价</view>
                        </view>
                    </view>
                    <view class="mdtp1" v-if="sctp">
                        <view class="weui-uploader__bd">
                            <view class="weui-uploader__files" id="uploaderFiles">
                                <view class="weui-uploader__file" v-for="(item, index) in images" :key="index">
                                    <view @tap="deleteImage" class="x" :data-index="index">
                                        <image src="https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/close.png"></image>
                                    </view>

                                    <image @tap="previewImage" class="weui-uploader__img" mode="aspectFill" :src="item"></image>
                                </view>
                            </view>
                            <view class="weui-uploader__input-box" v-if="images.length < 3">
                                <view @tap="chooseImage" class="weui-uploader__input"></view>
                            </view>
                        </view>
                        <view class="tpsl">{{ images.length }}/3</view>
                    </view>
                    <view class="goodlist">
                        <view class="good flex-row" v-for="(item, index) in good" :key="index">
                            <view class="flex-grow-1">{{ item.name }}</view>

                            <view class="flex-grow-0">x{{ item.number }}</view>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <view class="weui-btn-area">
            <button @tap="commentSubmit" class="weui-btn" :style="'background:' + color + ';font-size:35rpx;'" type="primary">提交评论</button>
        </view>
    </view>
</template>

<script>
import cardlist from '@/ymq_canyino2o/dist/cardlist/index';
import gettel from '@/ymq_canyino2o/dist/gettel/index';
var app = getApp();
var count = 0;
var imgArray = [];
var siteinfo = require('../../../siteinfo.js');
export default {
    components: {
        cardlist,
        gettel
    },
    data() {
        return {
            stars: [0, 1, 2, 3, 4],
            normalSrc: 'https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/no-star.png',
            selectedSrc: 'https://yunkeososyunchuc.bc8t.cn/ymq_canyino2o/static/full-star.png',
            key: 0,
            count: 0,
            images: [],
            sctp: false,
            order_id: '',
            good: '',

            orderinfo: {
                store: {
                    logo: '',
                    name: ''
                }
            },

            pjnr: '',
            halfSrc: '',
            color: ''
        };
    },
    onLoad: function (t) {
        app.globalData.setNavigationBarColor(this);
        var that = this;
        var e = uni.getStorageSync('users');
        console.log(t, e);
        that.setData({
            order_id: t.oid
        });
        uni.showLoading({
            title: '正在加载',
            mask: true
        });
        app.globalData.util.request({
            url: 'entry/wxapp/OrderInfo',
            data: {
                order_id: t.oid
            },
            success: function (t) {
                console.log(t);
                that.setData({
                    good: t.data.good,
                    orderinfo: t.data
                });
            }
        });
    },
    onReady: function () {},
    onShow: function () {},
    onHide: function () {},
    onUnload: function () {},
    onPullDownRefresh: function () {},
    onReachBottom: function () {},
    methods: {
        sctpFun: function () {
            this.setData({
                sctp: true
            });
        },

        selectLeft: function (t) {
            console.log('111111');
            var o = t.currentTarget.dataset.key;
            if (1 == this.key && 1 == t.currentTarget.dataset.key) {
                o = 0;
            }
            count = o;
            this.setData({
                key: o,
                count: count
            });
        },

        contentInput: function (t) {
            this.setData({
                pjnr: t.detail.value
            });
        },

        chooseImage: function (t) {
            var that = this;
            var e = this.images;
            var a = e.length;
            console.log(e);
            uni.chooseImage({
                count: 3 - a,
                success: function (t) {
                    e = e.concat(t.tempFilePaths);
                    that.setData({
                        images: e
                    });
                    console.log(e);
                }
            });
        },

        deleteImage: function (t) {
            var o = t.currentTarget.dataset.index;
            var e = this.images;
            console.log(o);
            e.splice(o, 1);
            this.setData({
                images: e
            });
            console.log(e);
        },

        commentSubmit: function (t) {
            var o = uni.getStorageSync('users').id;
            var e = this.orderinfo.store.id;
            var a = this.orderinfo.order.id;
            var n = this.pjnr;
            var s = this.count;
            var i = this.images;
            console.log(a, o, e, n, s, i);
            if (0 == s)
                uni.showModal({
                    title: '提示',
                    content: '请选择评分'
                });
            else if (null == n || '' == n) {
                uni.showModal({
                    title: '提示',
                    content: '请输入评价内容'
                });
            } else {
                var c = function () {
                    console.log('请求接口', imgArray, imgArray.toString());
                    app.globalData.util.request({
                        url: 'entry/wxapp/Assess',
                        cachetime: '0',
                        data: {
                            store_id: e,
                            user_id: o,
                            order_id: a,
                            stars: s,
                            content: n,
                            img: imgArray.toString()
                        },
                        success: function (t) {
                            if ('1' == t.data) {
                                uni.showModal({
                                    title: '提示',
                                    content: '提交成功'
                                });
                                setTimeout(function () {
                                    uni.redirectTo({
                                        url: 'order?status=4'
                                    });
                                }, 1000);
                            }
                            console.log('Assess', t.data);
                        }
                    });
                };
                uni.showLoading({
                    title: '正在提交',
                    mask: true
                });
                if (0 == i.length) {
                    c();
                } else {
                    (function t(o) {
                        var e = o.i ? o.i : 0;
                        var a = o.success ? o.success : 0;
                        var n = o.fail ? o.fail : 0;
                        uni.uploadFile({
                            url: o.url,
                            filePath: o.path[e],
                            name: 'upfile',
                            formData: null,
                            success: function (t) {
                                if ('' != t.data) {
                                    console.log(t);
                                    a++;
                                    imgArray.push(t.data);
                                    console.log(e);
                                    console.log('图片数组', imgArray);
                                } else {
                                    uni.showToast({
                                        icon: 'loading',
                                        title: '请重试'
                                    });
                                }
                            },
                            fail: function (t) {
                                n++;
                                console.log('fail:' + e + 'fail:' + n);
                            },
                            complete: function () {
                                console.log(e);
                                if (++e == o.path.length) {
                                    uni.hideToast();
                                    console.log('执行完毕');
                                    c();
                                    console.log('成功：' + a + ' 失败：' + n);
                                } else {
                                    console.log(e);
                                    o.i = e;
                                    o.success = a;
                                    o.fail = n;
                                    t(o);
                                }
                            }
                        });
                    })({
                        url: siteinfo.siteroot + '?i=' + siteinfo.uniacid + '&c=entry&a=wxapp&do=upload&m=ymq_canyino2o',
                        path: i
                    });
                }
            }
        },

        previewImage() {
            console.log('占位：函数 previewImage 未声明');
        }
    }
};
</script>
<style>
@import './pjdd.css';
</style>
